import re
from bs4 import BeautifulSoup
import requests
from time import sleep


def get_html(url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36 QIHU 360SE',
        'Referer': \
            'https://www.16df.xyz/home.html'}
    response = requests.get(url, params=headers)
    if response.status_code == 200:
        print("状态码成功")
        if "验证" in response.text:
            print("被验证")
            return None
        else:
            return response.text
    else:
        print("状态码失败" + url)
        print(response.status_code)


def parse_html(html, name):
    soup = BeautifulSoup(html, 'lxml')
    results = soup.select(name)
    with open('pachong3_csdn.txt', 'a+') as f:
        print('*' * 50 + '正在获取地址中' + '*' * 50)
        for result in results:
            url = 'https://www.16df.xyz'
            print(url + result['href'])
            f.write(url + result['href'] + '\n')
            print('*' * 50 + '下载成功' + '*' * 50)


def parse_html_full(html, name):
    soup = BeautifulSoup(html, 'lxml')
    results = soup.select(name)
    print('*' * 50 + '正在读取ing' + '*' * 50)
    with open('pachong4_csdn.txt', 'a+') as f:
        print('*' * 50 + '正在获取地址中' + '*' * 50)
        for result in results:
            if result['src']:
                print(result['src'])
                f.write(result['src'] + '\n')
                print('*' * 50 + '下载成功' + '*' * 50)
            else:
                pass


url = 'https://www.16df.xyz/pic/'
for i in range(1, 8):
    url_path = url + str(i)
    print(url_path + '获取html中')
    html = get_html(url_path)
    if html == None:
        print('失败')
        pass
    else:
        parse_html(html, name='a')
with open('pachong3_csdn.txt') as f:
    freadlines = f.readlines()
    for readline in freadlines:
        url_path_1 = readline.rstrip()
        if 'html' in url_path_1:
            print('*' * 50 + '正在获取' + url_path_1 + '*' * 50)
            html_1 = get_html(url_path_1)
            if html_1 == None:
                print('失败')
                pass
            else:
                parse_html_full(html_1, name='img')
                sleep(3)
        else:
            pass
